﻿using StudentManagementSystem.Data;
using StudentManagementSystem.Models;
using System;
using System.Linq;

namespace StudentManagementSystem
{
    class Program
    {
        static void Main(string[] args)
        {
           
            Console.WriteLine("学生管理系统启动中...");

            var dbContext = new DatabaseContext("StudentManagement.db");
            var schoolRepo = new SchoolRepository(dbContext.ConnectionString);
            var classRepo = new ClassRepository(dbContext.ConnectionString);
            var studentRepo = new StudentRepository(dbContext.ConnectionString);
            var logRepo = new LogRepository(dbContext.ConnectionString);

            bool running = true;

            while (running)
            {
                Console.WriteLine("\n请选择操作:");
                Console.WriteLine("1. 添加学校");
                Console.WriteLine("2. 查看所有学校");
                Console.WriteLine("3. 添加班级");
                Console.WriteLine("4. 查看班级");
                Console.WriteLine("5. 添加学生");
                Console.WriteLine("6. 查看学生");
                Console.WriteLine("7. 查看操作日志");
                Console.WriteLine("0. 退出");

                var choice = Console.ReadLine();

                switch (choice)
                {
                    case "1":
                        Console.WriteLine("请输入学校名称:");
                        var schoolName = Console.ReadLine();
                        Console.WriteLine("请输入学校地址:");
                        var schoolAddress = Console.ReadLine();

                        var school = new School { Name = schoolName, Address = schoolAddress };
                        schoolRepo.Add(school);

                        Console.WriteLine($"学校已添加，ID: {school.Id}");
                        break;

                    case "2":
                        var schools = schoolRepo.GetAll();
                        Console.WriteLine("所有学校:");
                        foreach (var s in schools)
                        {
                            Console.WriteLine($"ID: {s.Id}, 名称: {s.Name}, 地址: {s.Address}");

                            var classes = classRepo.GetBySchoolId(s.Id);
                            Console.WriteLine("  班级:");
                            foreach (var c in classes)
                            {
                                Console.WriteLine($"    ID: {c.Id}, 名称: {c.Name}");

                                var students = studentRepo.GetByClassId(c.Id);
                                Console.WriteLine("    学生:");
                                foreach (var st in students)
                                {
                                    Console.WriteLine($"      ID: {st.Id}, 姓名: {st.Name}, 年龄: {st.Age}");
                                }
                            }
                        }
                        break;

                    case "3":
                        Console.WriteLine("请输入所属学校ID:");
                        if (int.TryParse(Console.ReadLine(), out int schoolId))
                        {
                            Console.WriteLine("请输入班级名称:");
                            var className = Console.ReadLine();

                            var @class = new Class { Name = className, SchoolId = schoolId };
                            classRepo.Add(@class);

                            Console.WriteLine($"班级已添加，ID: {@class.Id}");
                        }
                        else
                        {
                            Console.WriteLine("无效的学校ID");
                        }
                        break;

                    case "4":
                        Console.WriteLine("请输入学校ID查看班级:");
                        if (int.TryParse(Console.ReadLine(), out int classSchoolId))
                        {
                            var classes = classRepo.GetBySchoolId(classSchoolId);
                            Console.WriteLine($"学校ID {classSchoolId} 的所有班级:");
                            foreach (var c in classes)
                            {
                                Console.WriteLine($"ID: {c.Id}, 名称: {c.Name}");
                            }
                        }
                        else
                        {
                            Console.WriteLine("无效的学校ID");
                        }
                        break;

                    case "5":
                        Console.WriteLine("请输入所属班级ID:");
                        if (int.TryParse(Console.ReadLine(), out int classId))
                        {
                            Console.WriteLine("请输入学生姓名:");
                            var studentName = Console.ReadLine();
                            Console.WriteLine("请输入学生年龄:");
                            if (int.TryParse(Console.ReadLine(), out int studentAge))
                            {
                                var student = new Student { Name = studentName, Age = studentAge, ClassId = classId };
                                studentRepo.Add(student);

                                Console.WriteLine($"学生已添加，ID: {student.Id}");
                            }
                            else
                            {
                                Console.WriteLine("无效的年龄");
                            }
                        }
                        else
                        {
                            Console.WriteLine("无效的班级ID");
                        }
                        break;

                    case "6":
                        Console.WriteLine("请输入班级ID查看学生:");
                        if (int.TryParse(Console.ReadLine(), out int studentClassId))
                        {
                            var students = studentRepo.GetByClassId(studentClassId);
                            Console.WriteLine($"班级ID {studentClassId} 的所有学生:");
                            foreach (var s in students)
                            {
                                Console.WriteLine($"ID: {s.Id}, 姓名: {s.Name}, 年龄: {s.Age}");
                            }
                        }
                        else
                        {
                            Console.WriteLine("无效的班级ID");
                        }
                        break;

                    case "7":
                        var logs = logRepo.GetAll();
                        Console.WriteLine("操作日志:");
                        foreach (var log in logs)
                        {
                            Console.WriteLine($"时间: {log.Timestamp}, 操作: {log.Action}, 详情: {log.Details}");
                        }
                        break;

                    case "0":
                        running = false;
                        Console.WriteLine("感谢使用学生管理系统！");
                        break;

                    default:
                        Console.WriteLine("无效的选择，请重新输入");
                        break;
                }
            }
        }
    }
}